import copy
import csv
import json
from re import L

with open("/home/jiang/桌面/obs_tools/bbbbbbb.json","r",encoding="utf-8") as file:
    un_packages=json.load(file)
# dd=list()
# dd=dict()
# for un_package in un_packages:
#     for key,va in un_package.items():
#         cc=list()
#         for v in va:
#             if "needed by"  in v:
#                 # cc.append([v.split()[0],v.split()[-1]])
#                 cc.append(v.split()[0])
#             else:
#                 cc.append(v)
#         dd[key]=cc
with open("w.json","r",encoding="utf-8") as file:
    dd=json.load(file)
# with open("/home/jiang/桌面/obs_tools/xxxxxxxx.json","r",encoding="utf-8") as file:
#     dd=json.load(file)


class ParseUn:
    def __init__(self) -> None:
        self.un_packages=dd
        
    def _get_unpackage(self,unpackage):
        return copy.deepcopy(self.un_packages.get(unpackage))
    
    def _get_single_df_row(self,pkg_name,names_set):
        pass
        
    def _write_csv_update_df_row(
            self,
            lst,
            pkg=None):
        """
        to write csv row and return new csv row list
        Args:
            lst:old csv row list
            res_type:install or build
            names_set:Store a list of package names that have been found
            pkg:package name

        Returns:
            new csv row list
        """
        local_lst = copy.deepcopy(lst)
        csv_writer = self.un_csv_writer
        if not pkg:
            csv_writer.writerow(local_lst)
            return lst
        local_lst.append(pkg)
        csv_writer.writerow(local_lst)
        return lst    
    
    
    def _data_to_csv(self,pkg_name,names_set):
        _stack=[self._get_unpackage(pkg_name)]
        if not _stack:
            print(pkg_name)
        df_row=[pkg_name]
        if not _stack:
            return
        while _stack:
            if not _stack[-1]:
                break
            next_pkg_aa = _stack[-1].pop()
            if  isinstance(next_pkg_aa,list):
                next_pkg=next_pkg_aa[0]
            else:
                next_pkg=next_pkg_aa
            if not next_pkg:
                continue
            # if next_pkg not in df_row and next_pkg not in names_set:
            if next_pkg not in df_row :
                names_set.add(next_pkg)
                df_row.append(next_pkg)
                if next_pkg =="emacs":
                    print("yyyy")
                depends = self._get_unpackage(next_pkg)
                if depends:
                    _stack.append(depends)
                else:
                    df_row = self._write_csv_update_df_row(
                        df_row)
                    df_row = df_row[:-1]
                    continue
            else:
                df_row = self._write_csv_update_df_row(df_row,
                                                        pkg=next_pkg)
                
                continue
        return  names_set
        
    def write_to_csv(self):
        with open("un.csv", "a+", encoding="utf-8", newline="") as un_csv:
            self.un_csv_writer = csv.writer(un_csv)
            name_set = set()
            for package_name in self.un_packages:
                name_set = self._data_to_csv(package_name,name_set)


ParseUn().write_to_csv()